﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetTraining.BLL;
using System.Web.SessionState;

namespace AspNetTraining.Account
{
    public partial class Login : Page
    {
        public UserAccount SessionLogin
        {
            get {
                return (UserAccount)Session["LoggedUser"];
            }
            set {
                Session["LoggedUser"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx";
            //OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];

            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }            
            
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            string uname = UserName.Text.Trim();
            string pwd = Password.Text.Trim();
            
            UserAccountManager userManager = new UserAccountManager();
            UserAccount loggedUser = userManager.GetUserByUserNamePassword(uname, pwd);
            if (loggedUser != null)
            {
                SessionLogin = loggedUser;

                if (Request.QueryString["returnUrl"] != null)
                    Response.Redirect(Request.QueryString["returnUrl"]);
                else
                    Response.Redirect("~/Default.aspx");
            }
            else
            {
                FailureText.Text = "The UserName or Password is not correct. Please try again.";
            }
        }

                     
       
    }
}